package com.price.httplib.websocket

import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

object WebSocketBuilder {

    var url: String? = null

    var timeSeconds = 40L

    var reConnectTime: Long? = null
    var reConnect: Boolean? = false

    var heart: String? = null
    var heartTime: Long = 5000

    var headerParams:Map<String,String>?=null

    var okHttpClient: OkHttpClient? = null

    var listener:MyWebSocketListener?=null

    inline fun build(func: WebSocketBuilder.() -> Unit): WebSocketManager {
        this.func()
        if (okHttpClient == null) {
            val okBuilder = OkHttpClient.Builder()
                .retryOnConnectionFailure(false)
                .readTimeout(timeSeconds, TimeUnit.SECONDS) //设置读取超时时间
                .writeTimeout(timeSeconds, TimeUnit.SECONDS) //设置写的超时时间
                .connectTimeout(timeSeconds, TimeUnit.SECONDS) //设置连接超时时间
                .callTimeout(timeSeconds,TimeUnit.SECONDS)
//                .pingInterval(heartTime, TimeUnit.MILLISECONDS)//ping关闭后会失败

            okHttpClient = okBuilder.build()
        }
        return WebSocketManager(this)
    }
}